package 面向对象实战购物车;

import java.util.Scanner;

public class 购物 {
    int n=0;
    public static void main(String[] args) {
        Good[] shopCar = new Good[100];
        while (true) {
            System.out.println("请输入以下命令进行操作：");
            System.out.println("添加商品到购物车：add");
            System.out.println("查询商品到购物车: query");
            System.out.println("修改商品购买数量: update");
            System.out.println("结算购买商品的金额：pay");
            Scanner sr = new Scanner(System.in);
            System.out.println("请输入命令");
            String cz = sr.next();
            switch (cz) {
                case "add":
                    addGood(shopCar, sr);
                    break;
                case "query":
                    queryGood(shopCar);
                    break;
                case "update":
                    updateGood(shopCar,sr);
                    break;
                case "pay":
                    payGood(shopCar);
                    break;
                default:
                    System.out.println("您输入的命令有错误");
                    break;
            }
        }
    }


    public static void payGood(Good[] shopCar) {
        double sum=0;
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i]!=null){
                sum+=shopCar[i].price*shopCar[i].buyNumber;
            }
        }
        System.out.println("您购物车的金额为："+sum);
    }

    public static void updateGood(Good[] shopCar,Scanner sr) {
            System.out.println("输入您需要修改的商品id:");
            int id=sr.nextInt();
            for (int i = 0; i < shopCar.length; i++) {
                if(shopCar[i]!=null){
                    if(id==shopCar[i].id){
                        System.out.println("请您输入修改后的"+shopCar[i].name+"数量:");
                        shopCar[i].buyNumber=sr.nextInt();
                        System.out.println("修改成功");
                        queryGood(shopCar);
                        return;
                    }
                }
            }
            System.out.println("您的购物车没有该商品的信息");
            updateGood(shopCar,sr);
    }

    public static void queryGood(Good[] shopCar) {
        System.out.println("=============查询购物车如下===========:");
        System.out.println("编号\t\t名称\t\t\t价格\t\t\t数量");
        for (int i = 0; i < shopCar.length; i++) {
            Good g=shopCar[i];
            if(g!=null){
                System.out.println(g.id+"\t\t"+g.name+"\t\t\t"+g.price+"\t\t\t"+g.buyNumber);
            }else {
                break;
            }
        }
    }

    public static void addGood(Good[] shopCar,Scanner sr) {
        System.out.println("输入商品编号(不重复):");
        int id=sr.nextInt();
        System.out.println("请您输入购买商品的名称：");
        String name=sr.next();
        System.out.println("请您输入购买数量:");
        int number=sr.nextInt();
        System.out.println("请您输入购买商品的价格");
        double price=sr.nextDouble();
        Good g=new Good();
        g.id=id;
        g.name=name;
        g.buyNumber=number;
        g.price=price;
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i]==null){
                shopCar[i]=g;
                break;
            }
        }
        System.out.println("您的商品"+name+"成功添加到购物车");
    }
}
